package com.tomgibara.android.veecheck.sample;

import com.tomgibara.android.veecheck.Veecheck;
import com.tomgibara.android.veecheck.util.PrefSettings;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SampleApplication extends Application {

	@Override
	public void onCreate() {
		super.onCreate();
		
		SharedPreferences prefs = PrefSettings.getSharedPrefs(this);
		//assign some default settings if necessary
		if (prefs.getString(PrefSettings.KEY_CHECK_URI, null) == null) {
			//some ridiculously fast polling, just to demonstrate it working...
			Editor editor = prefs.edit();
			editor.putBoolean(PrefSettings.KEY_ENABLED, true);
			editor.putLong(PrefSettings.KEY_PERIOD, 30 * 1000L);
			editor.putLong(PrefSettings.KEY_CHECK_INTERVAL, 60 * 1000L);
			editor.putString(PrefSettings.KEY_CHECK_URI, "http://www.tomgibara.com/android/veecheck/sample.xml");
			editor.commit();
		}

		//reschedule the checks - we need to do this if the settings have changed (as above)
		//it may also necessary in the case where an application has been updated
		//here for simplicity, we do it every time the application is launched
		Intent intent = new Intent(Veecheck.getRescheduleAction(this));
		sendBroadcast(intent);
	}
	
}
